import {LoginFieldType} from "./types/user.ts";
import {login, register} from "./api/auth.ts";

const TOKEN_KEY = '_token_';

export const getToken = () => sessionStorage.getItem(TOKEN_KEY);
export const setToken = (token: string) => sessionStorage.setItem(TOKEN_KEY, token);

export const handleLogin = async (data: LoginFieldType) => {
    const {token} = await login(data);
    setToken(token);
    return token
}

export const handleRegister = async (data: LoginFieldType) => {
    await register(data);
}
export const logout = async () => sessionStorage.removeItem(TOKEN_KEY)